include ApplicationHelper

class ContactProfilesController < ApplicationController
  before_filter :load_session

  #control general de acceso cancan, segun los permisos definidos en ability.rb
  authorize_resource :resource => :contact_profiles


  def load_session
    # sirve para que el poup de profile sepa donde setar los valores elegidos
    session[:profile_reference_id] = 'profile_reference_id'
    session[:profile_reference_type] = 'profile_reference_type'
    session[:profile_reference_name] = 'profile_reference_name'
    session[:profile_contact_id] = 'profile_contact_id'
    session[:profile_contact_name] = 'profile_contact_name'
  end

  def selector_list
    @items_per_page = count_record_page_selector

    if params[:search_type] == 'Thematic'
      @total, @thematics = Thematic.search_selector("", params[:page])
      render :partial=>'thematics/selector_thematics' , :layout=>false
    end

    if params[:search_type] == 'Country'
      @total, @countries = Country.search_selector("", params[:page])
      render :partial=>'countries/selector_countries' , :layout=>false
    end

    if params[:search_type] == ''
      render :partial=>'selector_empty_list' , :layout=>false
    end
  end

  def redirect_back
      redirect_back_or_default params[:url]
  end

  # GET /profiles
  # GET /profiles.xml
  def index
    save_redirect_back request.request_uri
    contact = Contact.find_by_user_id(current_user.id)
    if !contact.nil?
      @profiles, @total = Profile.search(contact.id, "", params[:page])
    else
      flash[:error] = 'El usuario no tiene contacto asociado'
      @profiles = nil
      @total = 0
    end

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @profiles }
    end
  end



  # GET /profiles/1
  # GET /profiles/1.xml
  def show
    @profile = Profile.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @profile }
    end
  end

  # GET /profiles/new
  # GET /profiles/new.xml
  def new
    @profile = Profile.new
    @contacts, @total = Contact.search("", params[:page])
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => new_contact_profile_path(@localuser) }
    end
  end

  # GET /profiles/1/edit
  def edit
    @profile = Profile.find(params[:id])
    @contacts, @total = Contact.search("", params[:page])

    if @profile.reference.nil?
       params[:profile_reference_name] = ""
    else
      if @profile.reference_type == 'Thematic'
        params[:profile_reference_name] = @profile.reference.name_to_local_language
      end
      if @profile.reference_type == 'Country'
        params[:profile_reference_name] = @profile.reference.name
      end
    end
    if @profile.contact.nil?
       params[:profile_contact_name] = ""
    else
       params[:profile_contact_name] = "#{@profile.contact.first_name} #{@profile.contact.last_name}"
    end
  end

  # POST /profiles
  # POST /profiles.xml
  def create
    @profile = Profile.new(params[:profile])
    @profile.contact = Contact.find_by_user_id(current_user.id)
    
    respond_to do |format|
      if @profile.save
        flash[:notice] = t('msg_record_successfully_created')
        format.html { redirect_to(contact_profile_path(@profile)) }
        format.xml  { render :xml => @profile, :status => :created, :location => @profile }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @profile.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /profiles/1
  # PUT /profiles/1.xml
  def update
    @profile = Profile.find(params[:id])

    respond_to do |format|
      if @profile.update_attributes(params[:profile])
        flash[:notice] = t('msg_record_successfully_updated')
        format.html { redirect_to(contact_profile_path(@profile)) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @profile.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /profiles/1
  # DELETE /profiles/1.xml
  def destroy
    @profile = Profile.find(params[:id])
    @profile.destroy

    respond_to do |format|
      format.html { redirect_to(contact_profiles_url) }
      format.xml  { head :ok }
    end
  end
end
